package com.liang.jiekou.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liang.jiekou.entity.*;

import com.liang.jiekou.entity.result.COPTCD;
import com.liang.jiekou.entity.result.MOCTAB;
import com.liang.jiekou.entity.result.PURTCD;
import com.liang.jiekou.mapper.MOCTAMapper;
import com.liang.jiekou.mapper.MOCTBMapper;
import com.liang.jiekou.service.MOCTAService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MOCTAServiceImpl implements MOCTAService {
@Resource
private MOCTAMapper moctaMapper;
@Resource
private MOCTBMapper moctbMapper;



    @Override
    public MOCTAB selectMOCTAMOCTB(String db, String dh) {
        MOCTA mocta =moctaMapper.findByDbWithDh(db,dh);
        List<MOCTB> moctbList = moctbMapper.getAllList(db, dh);

        //将查询的结果赋值给工单总类实体
        MOCTAB moctab = new MOCTAB();
        moctab.setMocta(mocta);

        moctab.setMoctbList(moctbList);

        return moctab;
    }

    @Override
    public IPage<MOCTAB> selectMOCTABall(Page<MOCTAB> page, String startcreate, String endcreate, String startupdate, String endupdate) {
        IPage<MOCTA> moctaiPage = moctaMapper.selectMOCTAall(page, startcreate, endcreate, startupdate, endupdate);
        List<MOCTA> moctaList = moctaiPage.getRecords();

        IPage<MOCTB> moctbiPage = moctbMapper.selectMOCTBall(page, startcreate, endcreate, startupdate, endupdate);
        List<MOCTB> moctbList = moctbiPage.getRecords();


        List<MOCTAB> moctabList = new ArrayList<>();

        for (MOCTA mocta : moctaList) {
            MOCTAB moctab = new MOCTAB();
            moctab.setMocta(mocta);

            List<MOCTB> matchedMoctbList = new ArrayList<>();
            for (MOCTB moctb : moctbList) {
                if (mocta.getTA001().equals(moctb.getTB001()) && mocta.getTA002().equals(moctb.getTB002())) {
                    matchedMoctbList.add(moctb);
                }
            }
            moctab.setMoctbList(matchedMoctbList);
            moctabList.add(moctab);

        }

        IPage<MOCTAB> resultPage = new Page<>(page.getCurrent(), page.getSize(), moctaiPage.getTotal());
        resultPage.setRecords(moctabList);

        return resultPage;
    }

    @Override
    public Page<MOCTAB> selectMoctabPage(Integer page, Integer pageSize, String startcreate, String endcreate, String startupdate, String endupdate) {

        List<MOCTA> moctaListAll =
                moctaMapper.selectMoctaListByCreateDateWithModiDate(startcreate,endcreate,startupdate,endupdate);
        List<MOCTAB>  moctabList = new ArrayList<>();
        int total = moctaListAll.size();
        int start = (page - 1 ) * pageSize;
        int end  = page * pageSize;
        //if(end > total) end = total;
        if (end <= total){
            for (int i = start; i < end; i++) {
                MOCTAB moctab = new MOCTAB();
                moctab.setMocta(moctaListAll.get(i));
                List<MOCTB> moctbList = moctaMapper.selectMoctbList(moctaListAll.get(i).getTA001(),moctaListAll.get(i).getTA002());
                moctab.setMoctbList(moctbList);
                moctabList.add(moctab);
            }
        }
        else if(end > total && start < end ){
            for (int i = start; i < end; i++) {
                MOCTAB moctab = new MOCTAB();
                moctab.setMocta(moctaListAll.get(i));
                List<MOCTB> moctbList = moctaMapper.selectMoctbList(moctaListAll.get(i).getTA001(),moctaListAll.get(i).getTA002());
                moctab.setMoctbList(moctbList);
                moctabList.add(moctab);
            }
        }



        Page<MOCTAB> moctabPage = new Page<>(page,pageSize);
        moctabPage.setRecords(moctabList);
        moctabPage.setTotal(moctaListAll.size());
        return moctabPage;
    }
}
